class Api::V1::LocationsController < Api::V1::BaseController
  before_action :authenticate_request!
  has_scope :company

  def index
    @locations = apply_scopes(Location).all
    @locaitons = paginate(@locations)
    render json: @locations, meta: paginate_meta(@locations)
  end

  def create
    begin
      @location = Location.new(create_params)
      authorize @location
      @location.save!
      render json: @location, status: 201
    rescue ActiveRecord::StatementInvalid
      @location.errors.add(:exec_error, 'Contact Administrator')
      error_render(@location, 403)
    rescue => err
      error_render(err.record, 403)
    end
  end

  def show
    @location = Location.find(params[:id])
    render json: @company, status: 200
  end

  def update
    begin
      @location = Location.find(params[:id])
      @location.update_attributes!(create_params)
      render json: @location, status: 200
    rescue => err
      error_render(err.record, 403)
    end
  end

  def destroy
    begin
      @location = Location.find(params[:id])
      @location.destroy!
      render status: 204
    rescue ActiveRecord::RecordNotFound
      render status: 404
    rescue => err
      error_render(err.record, 403)
    end
  end

  private

  def create_params
    params_parse(params)
  end

end
